<?php

  include_once("util.inc.php");
  include_once("recordset.inc.php");
  function displayEmpty() {
    echo "<p> No records to display...\n";
  }

  function setUpDisplay($title) {
    printHeader($title);
    cdbBody();
    cdbSetup();
  }

  function printTimeStamps($dataTime, $refreshLink) {
    //echo "IN PTS w/ dataTime = " . $dataTime . "\n";
    echo "<b><center>Current Time: " .date("r", time()) . "</center></b>\n";
    echo "<b><center>This data as of: {$dataTime} ";
    printLink($refreshLink, "(Refresh)");
    echo "</center></b>\n";
  }

  function printLink($linkLocation, $linkText) {
    echo "<a href={$linkLocation}>{$linkText}</a>";
  }

  function printNewLine() {
    echo "\n";
  }

  function prepPieGraph($data, $dataFieldIndex, $legendFieldIndex, $title, $index) {
    
    $_SESSION["pieGraph"]["{$index}"] = array();
    $_SESSION["pieGraph"]["{$index}"]["title"] = $title;
    $_SESSION["pieGraph"]["{$index}"]["data"] = array();
    $dataTotal = 0;
    for ($i=0; $i<count($data); $i++) {
      $cur = $data[$i];
      $_SESSION["pieGraph"]["{$index}"]["data"][$i] = (int)$cur[$dataFieldIndex];
      $dataTotal += (int)$cur[$dataFieldIndex];
      if (count($legendFieldIndex) > 1) {
        $legFldCt = 0;
        $curLeg = "";
        foreach($legendFieldIndex as $lf) {
          if($legFldCt>0) $curLeg = $curLeg . "/";
          $legFldCt++;
          $curLeg = $curLeg . $cur[$lf]; 
        }  
        $_SESSION["pieGraph"]["{$index}"]["legend"][$i] = $curLeg . " (%d)";
      } else {
        $_SESSION["pieGraph"]["{$index}"]["legend"][$i] = $cur[$legendFieldIndex] . " (%d)";
      }
    }

    return $dataTotal;
    
  }

  function prettyDisplayAggQuery($rs, 
                                 $numericFldIndex, 
                                 $labelFldIndices, 
                                 $title,
                                 $uniqueId) {
    
    echo "<center><font size=4><b>{$title}</b></font></center>\n";
    echo "<table width=100%>\n";
    echo "<tr>\n";
    echo "<td align=left>\n";
    $rs->printTable(1, $rs->recordCount);
    //printTable($colHeaders, $data, 0, count($data)-1);
    echo "</td>\n";
    echo "<td align=right>";
    if(prepPieGraph($rs->data, $numericFldIndex, $labelFldIndices, $title, $uniqueId)) {
      //echo "here";
      echo "<img src='./doPieGraph.php?index={$uniqueId}&dummy=" . time() . "'>";
    } else {
      //echo "or here";
      echo "<i>No chart to display</i>";
    }
    echo "</td></tr></table>";
  }
?>
